package com.example.bishe.toolclass;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.bishe.entity.Zsj_bzks;
import com.example.bishe.service.Zsj_bzksService;
import com.example.bishe.service.impl.Zsj_bzksServiceImpl;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/3/9 16:40
 */
@Component
public class StringTurnJson {//String转为JSONObject然后方便获取其中的键值对

    @Autowired
    Zsj_bzksService bzks = new Zsj_bzksServiceImpl();

    public JSONObject getJson(String result) {
        JSONObject jsonObject = new JSONObject();
        jsonObject = JSON.parseObject(String.valueOf(result));
        return jsonObject;
    }
    public JSON getJSON(HashMap<String,String> map){
        String str = JSON.toJSONString(map);
        JSONObject json = JSONObject.parseObject(String.valueOf(str));
        return json;
    }



    public String getPinyin(String china) {
        HanyuPinyinOutputFormat formart = new HanyuPinyinOutputFormat();
        formart.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        formart.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        formart.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] arrays = china.trim().toCharArray();
        String result = "";
        try {
            for (int i = 0; i < arrays.length; i++) {
                char ti = arrays[i];
                if (Character.toString(ti).matches("[\\u4e00-\\u9fa5]")) { //匹配是否是中文
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(ti, formart);
                    result += temp[0];
                } else {
                    result += ti;
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        String xun = xun(result);
        return xun;
    }
    public String xun(String result){
        String xun="";
        Zsj_bzks zsj_bzks = new Zsj_bzks();
        zsj_bzks.setEmail(result + "@zgxz3.wecom.work");
        if (bzks.select_bzks(zsj_bzks).length!=0){
            xun = xun(result + "1");
        }else
            xun = result + "@zgxz3.wecom.work";
        return xun;
    }
}
